#coding=utf-8
"""
Author:杭州多测师_付佳美
Time:2025/5/14 09:07
website:https://duoceshi.net
"""

# import pymysql
#
# # 通过pymysql库中的connect函数创建一个数据库的连接对象
# db = pymysql.connect(host="192.168.152.128",
#                      user="root",
#                      password='123456',
#                      database="dcs78",
#                      port=3306,
#                      charset='utf8')
#
# # 通过数据库的连接对象调用cursor方法，实例化一个游标对象出来
# # 游标对象有两个作用
# # 执行sql
# # 将执行出来的结果返回并保存
# cur = db.cursor()
#
# # 使用游标对象调用excute方法执行sql语句，并将查询结果保存
# cur.execute("select * from auto;")
#
# # 取出游标对象中的数据
# # 第一种方式fetchone()方法:取出游标对象中的第一条数据
# one = cur.fetchone()
# print(one)
# print(type(one))
#
# two = cur.fetchone()
# print(two)
#
# all = cur.fetchall()
# # 取出游标对象中的所有数据
# print(all)
#
# cur.execute('select * from auto;')
# all1 = cur.fetchall()
#
# cur.execute('insert into auto values(2,23,6,"xiaohuang",1844,90)')
# db.commit()
# 用数据库连接对象调用commit方法提交sql语句
# all3 = cur.fetchall()
# print(all3)

# import pymysql
#
# class Db_Utiles():
#
#     def __init__(self,host,user,password,database,port):
#         self.host = host
#         self.user = user
#         self.password = password
#         self.database = database
#         self.port = port
#         self.db = pymysql.connect(host=self.host,
#                                   user=self.user,
#                                   password=self.password,
#                                   database=self.database,
#                                   port=self.port,
#                                   charset='utf8')
#         self.cursor = self.db.cursor()
#
#     def find_all(self,tablename):
#
#         self.cursor.execute("select * from %s;" %tablename)
#         all = self.cursor.fetchall()
#         print(all)
#
#     def find_one(self,tablename,filed,value):
#
#         self.cursor.execute("select * from %s where %s=%s;" %(tablename,filed,value))
#         all = self.cursor.fetchall()
#         print(all)
#
#     def delete_one(self,tablename,filed,value):
#
#         self.cursor.execute('delete * from %s where %s=%s;' %(tablename,filed,value))
#         self.db.commit()
#
#     def delete_all(self,tablename):
#
#         self.cursor.execute('delete from %s' %(tablename))
#         self.db.commit()
#         self.db.close()
#
#     def insert_one(self,tablename,*value):
#         self.cursor.execute(f'insert into {tablename} values{value}')
#         self.db.commit()

# 内连接

# def select_inner_join(self,tablename1,tablename2,filed1,filed2):
#     self.cursor.execute(f'select * from {tablename1} inner join {tablename2} on {tablename1},{filed1}={tablename2}.{filed2};')
#     all = self.cursor.fetchall()
#     print(all)
#     print(type(all))
#
#     # 左连接
#
# def select_left_join(self,tablename1,tablename2,filed1,filed2):
#     self.cursor.execute(f'select * from {tablename1} inner join {tablename2} on {tablename1}.{filed1}={tablename2}.{filed2}')
#     all = self.cursor.fetchall()
#     print(all)
#     print(type(all))

    # 右连接
# def select_right_join(self,tablename1,tablename2,filed1,filed2):
#     self.cursor.execute(f'select * from {tablename1} inner join {tablename2} on {tablename1},{filed1}={tablename2}.{filed2}')
#     all = self.cursor.fetchall()
#     print(all)
#     print(type(all))




    # db.select_inner_join('dept', 'emp', 'dept1', 'dept2')  # 内连接
    # db.select_left_join('dept', 'emp', 'dept1', 'dept2')  # 左连接
    # db.select_right_join('dept', 'emp', 'dept1', 'dept2')  # 右连接




























    def insert_ones(self,tablename,filed1,filed2,*value):
        self.cursor.execute(f'insert into {tablename}({filed1},{filed2}) values{value}')

    def insert_zd(self,tablename,*values):
        clo = values[0::2]
        data = values[1::2]
        self.cursor.execute(f'insert into {tablename}({",".join(clo)}) values{tuple(data)};')
        self.db.commit()

if __name__ == '__main__':

    db = Db_Utiles('192.168.152.128',"root",'123456',"dcs78",3306)

    db.insert_zd('auto','name',"xiaofu","id",18)

































